package cn.yuanqiao.system.service.impl;

import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.system.domain.TenantOperLog;
import cn.yuanqiao.system.mapper.TenantOperLogMapper;
import cn.yuanqiao.system.service.ITenantOperLogService;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 操作日志 服务层处理
 *
 * @author ruoyi
 */
@Service
public class TenantOperLogServiceImpl implements ITenantOperLogService
{
    @Autowired
    private TenantOperLogMapper operLogMapper;

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     * @return 结果
     */
    @Override
    public int insertOperlog(TenantOperLog operLog)
    {
        if(operLog.getLogLevel() == null){
            if(operLog.getBusinessType() == 3){//删除
                operLog.setLogLevel(1); //警告
            }else {
                operLog.setLogLevel(operLog.getStatus());
            }
        }
        return operLogMapper.insertOperlog(operLog);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<TenantOperLog> selectOperLogList(TenantOperLog operLog)
    {
        return operLogMapper.selectOperLogList(operLog);
    }

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Long[] operIds)
    {
        int i = operLogMapper.selectOperLogcout(operIds, new TenantOperLog().getTableName());
        if (i>0){
            throw new ServiceException("未审计的数据不可以删除,请先进行审计!");
        }

        return operLogMapper.deleteOperLogByIds(operIds,new TenantOperLog().getTableName());
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public TenantOperLog selectOperLogById(Long operId)
    {
        return operLogMapper.selectOperLogById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog()
    {
        operLogMapper.cleanOperLog(new TenantOperLog().getTableName());
    }

    @Override
    public List<TenantOperLog> selectOperLogListBySysauditor(TenantOperLog operLog) {
        return operLogMapper.selectOperLogListBySysauditor(operLog);
    }

    @Override
    public List<TenantOperLog> selectOperLogListBySyssso(TenantOperLog operLog) {
        return operLogMapper.selectOperLogListBySyssso(operLog);
    }

    @Override
    public List<Long[]> operLogTongjiBySysauditor(TenantOperLog operLog) {
        Map<String,Long> map=operLogMapper.operLogTongjiBySysauditor(operLog);
        List list=new ArrayList(6);
        for (Map.Entry<String,Long> entry : map.entrySet()) {
            Long [] value=new Long[3];
            value[0]=Long.valueOf(String.valueOf(entry.getKey().charAt(0)));
            value[1]=Long.valueOf(String.valueOf(entry.getKey().charAt(1)));
            value[2]=Long.valueOf(String.valueOf(entry.getValue()));
            list.add(value);
        }
        return list;
    }

    @Override
    public List<Long[]> operLogTongjiBySyssso(TenantOperLog operLog) {
        Map<String,Long> map=operLogMapper.operLogTongjiBySyssso(operLog);;
        List list=new ArrayList(6);
        for (Map.Entry<String,Long> entry : map.entrySet()) {
            Long [] value=new Long[3];
            value[0]=Long.valueOf(String.valueOf(entry.getKey().charAt(0)));
            value[1]=Long.valueOf(String.valueOf(entry.getKey().charAt(1)));
            value[2]=Long.valueOf(String.valueOf(entry.getValue()));
            list.add(value);
        }
        return list;
    }

    @Override
    public List<Long[]> operLogTongji(TenantOperLog operLog) {
        Map<String,Long> map=operLogMapper.operLogTongji(operLog);
        List list=new ArrayList(6);
        for (Map.Entry<String,Long> entry : map.entrySet()) {
            Long [] value=new Long[3];
            value[0]=Long.valueOf(String.valueOf(entry.getKey().charAt(0)));
            value[1]=Long.valueOf(String.valueOf(entry.getKey().charAt(1)));
            value[2]=Long.valueOf(String.valueOf(entry.getValue()));
            list.add(value);
        }
        return list;
    }

    /**
     * 修改审计状态
     * @param jsonObject
     * @return
     */
    @Override
    public int updatAuditByIds(JSONObject jsonObject) {
        JSONArray ids = jsonObject.getJSONArray("operIds");
        String auditOpinion = jsonObject.getString("auditOpinion");
        List<Long> longs = ids.toJavaList(Long.class);
        return operLogMapper.updatAuditByIds(longs, new TenantOperLog().getTableName(), auditOpinion);
    }
}
